################################################################################
## Replication script 
## Article: Democracy, Autocracy, and the Design of International Organizations
## Authors: Jonas Tallberg and Carl Vikberg
## R version 4.3.2
################################################################################


setwd("") # Set the desired working directory

options(scipen=999) # Turn off scientific notation

#### Load necessary packages

library(tidyverse) # 2.0.0
library(fixest) # 0.11.2
library(patchwork) # 1.2.0
library(marginaleffects) # 0.18.0
library(modelsummary) # 2.0.0
library(tinytable) # 0.2.1



#### Import datasets

combandmia2 <- read_csv("maindata.csv") # This is the main dataset - used in most analyses.
combandmia2b <- read_csv("datafullmia.csv") # This is a dataset including the full Measuring International Authority (MIA) sample of IOs. It is used for robustness checks in Figure A11 and Table A18


################################ Main text ##################################

# These are the main data for graphs in Figure 1. All IOs continuously active between 1975 and 2019.

yeardatacompl2 <- combandmia2 %>% filter(year > 1974) %>% 
  group_by(ionumber) %>%
  filter(any(year == 1975) & any(year == 2019)) %>%
  filter(any(!is.na(pooling))) %>%
  group_by(year) %>%
  summarize(meandem = mean(avgelec, na.rm = T), # Avg democratic density in all IOs
            meandemTS = mean(avgelec[contract == "task specific"], na.rm = T),  # Avg democratic density in TS IOs
            meandemGP = mean(avgelec[contract == "general purpose"], na.rm = T),  # Avg democratic density in GP IOs
            meandel = mean(delegation, na.rm = T), # Avg delegation in all IOs
            meandelTS = mean(delegation[contract == "task specific"], na.rm = T), # Avg delegation in TS IOs
            meandelGP = mean(delegation[contract == "general purpose"], na.rm = T), # Avg delegation in GP IOs
            meanpool = mean(pooling, na.rm = T), # Avg pooling in all IOs
            meanpoolTS = mean(pooling[contract == "task specific"], na.rm = T), # Avg pooling in TS IOs
            meanpoolGP = mean(pooling[contract == "general purpose"], na.rm = T), # Avg pooling in GP IOs
            meanacc = mean(accessio, na.rm = T), # Avg access in all IOs
            meanaccTS = mean(accessio[contract == "task specific"], na.rm = T), # Avg access in TS IOs
            meanaccGP = mean(accessio[contract == "general purpose"], na.rm = T), # Avg access in GP IOs
            nio = n()) # Number of IOs.


### Figure 1

p1 <- ggplot(yeardatacompl2, aes(x = year)) +
  geom_line(aes(y = meanpool, linetype = "Design")) +
  geom_line(aes(y = meandem, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y.right = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y.right = element_blank(), axis.ticks.y.right = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Pooling", limits = c(0,1), breaks = seq(0, 1, by = 0.5), 
                     sec.axis = sec_axis(~. , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid"))

p2 <- ggplot(yeardatacompl2, aes(x = year)) +
  geom_line(aes(y = meandel, linetype = "Design")) +
  geom_line(aes(y = meandem, linetype = "Democracy")) + 
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(), panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y.right = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y.right = element_blank(), axis.ticks.y.right = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Delegation", limits = c(0,1), breaks = seq(0, 1, by = 0.5), 
                     sec.axis = sec_axis(~. , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid"))+
  ggtitle("All IOs")


p3 <- ggplot(yeardatacompl2,aes(x = year)) +
  geom_line(aes(y = meanacc, linetype = "Design")) +
  geom_line(aes(y = meandem*1.5, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y.right = element_blank(), axis.text.y.right = element_blank(), axis.ticks.y.right = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Access", limits = c(0,1.5), breaks = seq(0, 1.5, by = 0.75),
                     sec.axis = sec_axis(~./1.5 , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid"))


p4 <- ggplot(yeardatacompl2,aes(x = year)) +
  geom_line(aes(y = meanpoolGP, linetype = "Design")) +
  geom_line(aes(y = meandemGP, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y.right = element_blank(), axis.ticks.y.right = element_blank(),
        axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Pooling", limits = c(0,1), breaks = seq(0, 1, by = 0.5),
                     sec.axis = sec_axis(~. , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid"))


p5 <- ggplot(yeardatacompl2,aes(x = year)) +
  geom_line(aes(y = meandelGP, linetype = "Design")) +
  geom_line(aes(y = meandemGP, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y.right = element_blank(), axis.ticks.y.right = element_blank(),
        axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Delegation", limits = c(0,1), breaks = seq(0, 1, by = 0.5),
                     sec.axis = sec_axis(~. ,breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid")) +
  ggtitle("General-purpose")


p6 <- ggplot(yeardatacompl2,aes(x = year)) +
  geom_line(aes(y = meanaccGP, linetype = "Design")) +
  geom_line(aes(y = meandemGP*1.5, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y = element_blank(),
        axis.text.y.right = element_blank(), axis.ticks.y.right = element_blank(),
        axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Access", limits = c(0,1.5), breaks = seq(0, 1.5, by = 0.75),
                     sec.axis = sec_axis(~./1.5 , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid"))

p7 <- ggplot(yeardatacompl2,aes(x = year)) +
  geom_line(aes(y = meanpoolTS, linetype = "Design")) +
  geom_line(aes(y = meandemTS, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y.left = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Pooling", limits = c(0,1), breaks = seq(0, 1, by = 0.5),
                     sec.axis = sec_axis(~. , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid")) 

p8 <- ggplot(yeardatacompl2,aes(x = year)) +
  geom_line(aes(y = meandelTS, linetype = "Design")) +
  geom_line(aes(y = meandemTS, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y.left = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
        axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Delegation", limits = c(0,1), breaks = seq(0, 1, by = 0.5),
                     sec.axis = sec_axis(~. , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid"))+
  ggtitle("Task-specific")


p9 <- ggplot(yeardatacompl2,aes(x = year)) +
  geom_line(aes(y = meanaccTS, linetype = "Design")) +
  geom_line(aes(y = meandemTS*1.5, linetype = "Democracy")) +
  theme(aspect.ratio = 1, panel.grid = element_blank(),panel.background = element_blank(),panel.border = element_rect(colour = "black", fill=NA),
        legend.key=element_blank(), axis.title = element_text(size = 10), axis.title.x = element_blank(), axis.text = element_text(colour = "black"),
        axis.title.y.left = element_blank(), 
        axis.text.y.left = element_blank(), axis.ticks.y.left = element_blank()) +
  scale_x_continuous(limits = c(1975,2019), breaks = seq(1975, 2019, by = 10)) +
  scale_y_continuous(name = "Access", limits = c(0,1.5), breaks = seq(0, 1.5, by = 0.75),
                     sec.axis = sec_axis(~./1.5 , breaks = seq(0, 1, by = 0.5),
                                         name = "Democracy")) +  
  scale_linetype_manual(name = "", values = c("Democracy"="dashed","Design"="solid"))

p2+p1+p3+p5+p4+p6+p8+p7+p9+plot_layout(nrow = 3, byrow = FALSE, guides = "collect") & 
  theme(legend.position = "bottom", legend.box.background = element_rect(colour = "black"), plot.title = element_text(size = 10, hjust = 0.5, face = "bold"))

ggsave("Figure1.tiff", width = 6.5, height = 6.5, dpi = 600, scale = 7/6.5, compression = "lzw")



### Table 1

# Direct relationship

poolmodel <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodel <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodel <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


# Interaction

poolmodelint <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelint <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelint <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


# Construct table

modelsummary(list(delmodel, poolmodel, accmodel, delmodelint, poolmodelint,  accmodelint),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3elecdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.")



### Figure 2

poolplot <- plot_comparisons(poolmodelint, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(poolmodelint, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ # Adds degrees of freedom correction to adjust p-values
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Pooling") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplot <- plot_comparisons(delmodelint, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(delmodelint, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ # Adds degrees of freedom correction to adjust p-values
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Delegation") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

accplot <- plot_comparisons(accmodelint, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(accmodelint, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ # Adds degrees of freedom correction to adjust p-values
  scale_y_continuous(name = "Marginal effect",limits = c(-0.15,0.3)) + ggtitle("Access") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplot + poolplot + accplot + plot_layout(axis_titles = "collect") & theme(axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
                                                                            axis.title = element_text(size = 15))

ggsave("Figure2.tiff", width = 6.5, height = (6.5/11)*4, dpi = 600, scale = 11/6.5, compression = "lzw")



############################### Online Appendix #####################################

### Figure A1
# Show distribution of membership in GP vs TS

ggplot(combandmia2, aes(x=avgelec, after_stat(count), fill = contract)) + geom_density(alpha=0.25, bounds = c(0,1)) + labs(fill = "Governance purpose") + theme_bw() + scale_fill_manual(values=c("black", "white"), labels = c("General-purpose", "Task-specific")) + 
  scale_x_continuous(name = "Democratic density", limits = c(0,1)) + scale_y_continuous(name = "Density (scaled by group size)") + theme(panel.grid = element_blank())


ggsave("FigureA1.tiff", width = 10, height = 7, dpi = 600, compression = "lzw")



### Table A2

# Calculate proportion of changes in dem dens due to changes to membership vs changes within membership

propsa <- combandmia2 %>%
  group_by(ionumber) %>%
  summarize(demtotal = sum(avgelec - dplyr::lag(avgelec) > 0, na.rm = T),
            demandentry = sum(avgelec - dplyr::lag(avgelec) > 0 & anyentry == 1, na.rm = T),
            demandexit = sum(avgelec - dplyr::lag(avgelec) > 0 & anyexit == 1, na.rm = T),
            demandstable  = sum(avgelec - dplyr::lag(avgelec) > 0 & anyentry == 0 & anyexit == 0, na.rm = T),
            demandchange  = sum(avgelec - dplyr::lag(avgelec) > 0 & (anyentry == 1 | anyexit == 1), na.rm = T),
            auttotal = sum(avgelec - dplyr::lag(avgelec) < 0, na.rm = T),
            autandentry = sum(avgelec - dplyr::lag(avgelec) < 0 & anyentry == 1, na.rm = T),
            autandexit = sum(avgelec - dplyr::lag(avgelec) < 0 & anyexit == 1, na.rm = T),
            autandstable  = sum(avgelec - dplyr::lag(avgelec) < 0 & anyentry == 0 & anyexit == 0, na.rm = T),
            autandchange  = sum(avgelec - dplyr::lag(avgelec) < 0 & (anyentry == 1 | anyexit == 1), na.rm = T),
            changetotal = sum(avgelec - dplyr::lag(avgelec) != 0, na.rm = T),
            changeandentry = sum(avgelec - dplyr::lag(avgelec) != 0  & anyentry == 1, na.rm = T),
            changeandexit = sum(avgelec - dplyr::lag(avgelec) != 0  & anyexit == 1, na.rm = T),
            changeandstable  = sum(avgelec - dplyr::lag(avgelec) != 0 & anyentry == 0 & anyexit == 0, na.rm = T),
            changeandchange = sum(avgelec - dplyr::lag(avgelec) != 0 & (anyentry == 1 | anyexit == 1), na.rm = T))


propsa2 <- propsa %>%
  summarize(across(2:16, sum, na.rm = TRUE))

table <- propsa2 %>%
  dplyr::select(demandchange, demandstable, demtotal, autandchange, autandstable, auttotal, changeandchange, changeandstable, changetotal)

datasummary_df(table, fmt = 0)







######### Robustness checks

#### Alternative operationalizations of independent variable

### Table A3; A4


## Liberal democracy

# Direct relationship

poolmodela <- feols(pooling ~ lag3libdem + i(lag3contract, ref = "task specific") + lag3sdlibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodela <- feols(delegation ~ lag3libdem + i(lag3contract, ref = "task specific") + lag3sdlibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodela <- feols(accessio ~ lag3libdem + i(lag3contract, ref = "task specific") + lag3sdlibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelinta <- feols(pooling ~ lag3libdem*i(lag3contract, ref = "task specific") + lag3sdlibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelinta <- feols(delegation ~ lag3libdem*i(lag3contract, ref = "task specific") + lag3sdlibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelinta <- feols(accessio ~ lag3libdem*i(lag3contract, ref = "task specific") + lag3sdlibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Participatory democracy

# Direct relationship

poolmodelb <- feols(pooling ~ lag3partipdem + i(lag3contract, ref = "task specific") + lag3sdpartipdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelb <- feols(delegation ~ lag3partipdem + i(lag3contract, ref = "task specific") + lag3sdpartipdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelb <- feols(accessio ~ lag3partipdem + i(lag3contract, ref = "task specific") + lag3sdpartipdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintb <- feols(pooling ~ lag3partipdem*i(lag3contract, ref = "task specific") + lag3sdpartipdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintb <- feols(delegation ~ lag3partipdem*i(lag3contract, ref = "task specific") + lag3sdpartipdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintb <- feols(accessio ~ lag3partipdem*i(lag3contract, ref = "task specific") + lag3sdpartipdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Deliberative democracy

# Direct relationship

poolmodelc <- feols(pooling ~ lag3delibdem + i(lag3contract, ref = "task specific") + lag3sddelibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelc <- feols(delegation ~ lag3delibdem + i(lag3contract, ref = "task specific") + lag3sddelibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelc <- feols(accessio ~ lag3delibdem + i(lag3contract, ref = "task specific") + lag3sddelibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintc <- feols(pooling ~ lag3delibdem*i(lag3contract, ref = "task specific") + lag3sddelibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintc <- feols(delegation ~ lag3delibdem*i(lag3contract, ref = "task specific") + lag3sddelibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintc <- feols(accessio ~ lag3delibdem*i(lag3contract, ref = "task specific") + lag3sddelibdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Egalitarian democracy

# Direct relationship

poolmodeld <- feols(pooling ~ lag3egaldem + i(lag3contract, ref = "task specific") + lag3sdegaldem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodeld <- feols(delegation ~ lag3egaldem + i(lag3contract, ref = "task specific") + lag3sdegaldem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodeld <- feols(accessio ~ lag3egaldem + i(lag3contract, ref = "task specific") + lag3sdegaldem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintd <- feols(pooling ~ lag3egaldem*i(lag3contract, ref = "task specific") + lag3sdegaldem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintd <- feols(delegation ~ lag3egaldem*i(lag3contract, ref = "task specific") + lag3sdegaldem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintd <- feols(accessio ~ lag3egaldem*i(lag3contract, ref = "task specific") + lag3sdegaldem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Share of democracies

# Direct relationship

poolmodele <- feols(pooling ~ lag3sharedemoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodele <- feols(delegation ~ lag3sharedemoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodele <- feols(accessio ~ lag3sharedemoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelinte <- feols(pooling ~ lag3sharedemoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelinte <- feols(delegation ~ lag3sharedemoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelinte <- feols(accessio ~ lag3sharedemoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


### Table A3

modelsummary(list("Delegation" = delmodela, "Pooling" = poolmodela, "Access" = accmodela, "Delegation" = delmodelb, "Pooling" = poolmodelb, "Access" = accmodelb, 
                  "Delegation" = delmodelc, "Pooling" = poolmodelc, "Access" = accmodelc, "Delegation" = delmodeld, "Pooling" = poolmodeld, "Access" = accmodeld, 
                  "Delegation" = delmodele, "Pooling" = poolmodele, "Access" = accmodele),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3libdem" = "Democratic density", "lag3sdlibdem" = "Regime heterogeneity",
                          "lag3partipdem" = "Democratic density", "lag3sdpartipdem" = "Regime heterogeneity",
                          "lag3delibdem" = "Democratic density", "lag3sddelibdem" = "Regime heterogeneity",
                          "lag3egaldem" = "Democratic density", "lag3sdegaldem" = "Regime heterogeneity",
                          "lag3sharedemoc" = "Democratic density", 
                          "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Liberal" = 2:4, "Participatory" = 5:7, "Deliberative" = 8:10, "Egalitarian" = 11:13, "Share democratic members" = 14:16))


### Table A4

modelsummary(list("Delegation" = delmodelinta, "Pooling" = poolmodelinta, "Access" = accmodelinta, "Delegation" = delmodelintb, "Pooling" = poolmodelintb, "Access" = accmodelintb, 
                  "Delegation" = delmodelintc, "Pooling" = poolmodelintc, "Access" = accmodelintc, "Delegation" = delmodelintd, "Pooling" = poolmodelintd, "Access" = accmodelintd, 
                  "Delegation" = delmodelinte, "Pooling" = poolmodelinte, "Access" = accmodelinte),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3libdem" = "Democratic density", "lag3libdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdlibdem" = "Regime heterogeneity",
                          "lag3partipdem" = "Democratic density", "lag3partipdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdpartipdem" = "Regime heterogeneity",
                          "lag3delibdem" = "Democratic density", "lag3delibdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sddelibdem" = "Regime heterogeneity",
                          "lag3egaldem" = "Democratic density", "lag3egaldem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdegaldem" = "Regime heterogeneity",
                          "lag3sharedemoc" = "Democratic density", "lag3sharedemoc:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)",
                          "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Liberal" = 2:4, "Participatory" = 5:7, "Deliberative" = 8:10, "Egalitarian" = 11:13, "Share democratic members" = 14:16))


### Figure A2

# Create mfx for interaction

poolmodelintaplot <- avg_comparisons(poolmodelinta, type = "response", variables = list("lag3libdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelinta, type = "wald"))
poolmodelintbplot <- avg_comparisons(poolmodelintb, type = "response", variables = list("lag3partipdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintb, type = "wald"))
poolmodelintcplot <- avg_comparisons(poolmodelintc, type = "response", variables = list("lag3delibdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintc, type = "wald"))
poolmodelintdplot <- avg_comparisons(poolmodelintd, type = "response", variables = list("lag3egaldem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintd, type = "wald"))
poolmodelinteplot <- avg_comparisons(poolmodelinte, type = "response", variables = list("lag3sharedemoc" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelinte, type = "wald"))

delmodelintaplot <- avg_comparisons(delmodelinta, type = "response", variables = list("lag3libdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelinta, type = "wald"))
delmodelintbplot <- avg_comparisons(delmodelintb, type = "response", variables = list("lag3partipdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintb, type = "wald"))
delmodelintcplot <- avg_comparisons(delmodelintc, type = "response", variables = list("lag3delibdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintc, type = "wald"))
delmodelintdplot <- avg_comparisons(delmodelintd, type = "response", variables = list("lag3egaldem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintd, type = "wald"))
delmodelinteplot <- avg_comparisons(delmodelinte, type = "response", variables = list("lag3sharedemoc" = 0.1), by = "lag3contract", df = insight::get_df(delmodelinte, type = "wald"))

accmodelintaplot <- avg_comparisons(accmodelinta, type = "response", variables = list("lag3libdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelinta, type = "wald"))
accmodelintbplot <- avg_comparisons(accmodelintb, type = "response", variables = list("lag3partipdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintb, type = "wald"))
accmodelintcplot <- avg_comparisons(accmodelintc, type = "response", variables = list("lag3delibdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintc, type = "wald"))
accmodelintdplot <- avg_comparisons(accmodelintd, type = "response", variables = list("lag3egaldem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintd, type = "wald"))
accmodelinteplot <- avg_comparisons(accmodelinte, type = "response", variables = list("lag3sharedemoc" = 0.1), by = "lag3contract", df = insight::get_df(accmodelinte, type = "wald"))


# Create legend data

legend_data_democracyindices <- data.frame(
  term = c("lag3libdem", "lag3partipdem", "lag3delibdem", "lag3egaldem", "lag3sharedemoc"), 
  label = c("Liberal democracy", "Participatory democracy", "Deliberative democracy", "Egalitarian democracy", "Share democratic members"),
  shape = c(16, 15, 17, 18, 0))


# Create interaction plots

paltdempool <- ggplot(poolmodelintaplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.1)) +
  geom_pointrange(data = poolmodelintbplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = poolmodelintcplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0)) +
  geom_pointrange(data = poolmodelintdplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.05)) +
  geom_pointrange(data = poolmodelinteplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.1)) + theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_democracyindices$term, values = legend_data_democracyindices$shape, labels = legend_data_democracyindices$label) +
  ggtitle("Pooling")

paltdemdel <- ggplot(delmodelintaplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.1)) +
  geom_pointrange(data = delmodelintbplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = delmodelintcplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0)) +
  geom_pointrange(data = delmodelintdplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.05)) +
  geom_pointrange(data = delmodelinteplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.1)) + theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_democracyindices$term, values = legend_data_democracyindices$shape, labels = legend_data_democracyindices$label) +
  ggtitle("Delegation")

paltdemacc <- ggplot(accmodelintaplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.3, 0.4), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.1)) +
  geom_pointrange(data = accmodelintbplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = accmodelintcplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0)) +
  geom_pointrange(data = accmodelintdplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.05)) +
  geom_pointrange(data = accmodelinteplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.1)) + theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_democracyindices$term, values = legend_data_democracyindices$shape, labels = legend_data_democracyindices$label) +
  ggtitle("Access")

paltdemdel + paltdempool + paltdemacc + 
  plot_layout(ncol = 3, axis_titles = "collect", guides = "collect") & 
  theme(legend.position = "bottom", axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
        axis.title = element_text(size = 15), legend.text = element_text(size = 12))

ggsave("FigureA2.tiff", width = 14, height = 5.5, dpi = 600, compression = "lzw")


#### Democratic density measured using polity

### Table A5

# Direct relationship

poolmodelpolity <- feols(pooling ~ lag3meanpolity + i(lag3contract, ref = "task specific") + lag3sdpolity + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelpolity <- feols(delegation ~ lag3meanpolity + i(lag3contract, ref = "task specific") + lag3sdpolity + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelpolity <- feols(accessio ~ lag3meanpolity + i(lag3contract, ref = "task specific") + lag3sdpolity + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


# Interaction

poolmodelintpolity <- feols(pooling ~ lag3meanpolity*i(lag3contract, ref = "task specific") + lag3sdpolity + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintpolity <- feols(delegation ~ lag3meanpolity*i(lag3contract, ref = "task specific") + lag3sdpolity + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintpolity <- feols(accessio ~ lag3meanpolity*i(lag3contract, ref = "task specific") + lag3sdpolity + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


# Create table
modelsummary(list("Delegation" = delmodelpolity, "Pooling" = poolmodelpolity, "Access" = accmodelpolity, "Delegation" = delmodelintpolity, "Pooling" = poolmodelintpolity, "Access" = accmodelintpolity),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3meanpolity" = "Democratic density (Polity)", "lag3meanpolity:lag3contract::general purpose" = "Democratic density (Polity)*Governance purpose (ref: Task-specific)", "lag3sdpolity" = "Regime heterogeneity (Polity)", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.")


### Figure A3

## Marginal effects of 1 unit increase, NB!

poolplotpolity <- plot_comparisons(poolmodelintpolity, type = "response", variables = list("lag3meanpolity" = 1), condition = "lag3contract", df = insight::get_df(poolmodelintpolity, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Pooling") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotpolity <- plot_comparisons(delmodelintpolity, type = "response", variables = list("lag3meanpolity" = 1), condition = "lag3contract", df = insight::get_df(delmodelintpolity, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Delegation") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

accplotpolity <- plot_comparisons(accmodelintpolity, type = "response", variables = list("lag3meanpolity" = 1), condition = "lag3contract", df = insight::get_df(accmodelintpolity, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.15,0.3)) + ggtitle("Access") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotpolity + poolplotpolity + accplotpolity + plot_layout(axis_titles = "collect") & theme(axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
                                                                                              axis.title = element_text(size = 15))

ggsave("FigureA3.tiff", width = 11, height = 4, dpi = 600, compression = "lzw")


#### Additional operationalizations of independent variable.

### Table A6; A7

## Share of MS undergoing democratization episode

# Direct relationship

poolmodeldemocratiz <- feols(pooling ~ lag3sharedemocratize + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodeldemocratiz <- feols(delegation ~ lag3sharedemocratize + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodeldemocratiz <- feols(accessio ~ lag3sharedemocratize + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintdemocratiz <- feols(pooling ~ lag3sharedemocratize*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintdemocratiz <- feols(delegation ~ lag3sharedemocratize*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintdemocratiz <- feols(accessio ~ lag3sharedemocratize*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Share of MS undergoing autocratization episode

# Direct relationship

poolmodelautocratiz <- feols(pooling ~ lag3shareautocratize + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelautocratiz <- feols(delegation ~ lag3shareautocratize + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelautocratiz <- feols(accessio ~ lag3shareautocratize + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintautocratiz <- feols(pooling ~ lag3shareautocratize*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintautocratiz <- feols(delegation ~ lag3shareautocratize*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintautocratiz <- feols(accessio ~ lag3shareautocratize*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Share of MS with stable democ regime

# Direct relationship

poolmodelstabledemoc <- feols(pooling ~ lag3sharestabledemoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelstabledemoc <- feols(delegation ~ lag3sharestabledemoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelstabledemoc <- feols(accessio ~ lag3sharestabledemoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintstabledemoc <- feols(pooling ~ lag3sharestabledemoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintstabledemoc <- feols(delegation ~ lag3sharestabledemoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintstabledemoc <- feols(accessio ~ lag3sharestabledemoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Share of MS with stable autoc regime

# Direct relationship

poolmodelstableautoc <- feols(pooling ~ lag3sharestableautoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelstableautoc <- feols(delegation ~ lag3sharestableautoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelstableautoc <- feols(accessio ~ lag3sharestableautoc + i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintstableautoc <- feols(pooling ~ lag3sharestableautoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintstableautoc <- feols(delegation ~ lag3sharestableautoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintstableautoc <- feols(accessio ~ lag3sharestableautoc*i(lag3contract, ref = "task specific") + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


### Table A6

modelsummary(list("Delegation" = delmodeldemocratiz, "Pooling" = poolmodeldemocratiz, "Access" = accmodeldemocratiz, 
                  "Delegation" = delmodelstabledemoc, "Pooling" = poolmodelstabledemoc,  "Access" = accmodelstabledemoc, 
                  "Delegation" = delmodelautocratiz, "Pooling" = poolmodelautocratiz,  "Access" = accmodelautocratiz, 
                  "Delegation" = delmodelstableautoc, "Pooling" = poolmodelstableautoc,  "Access" = accmodelstableautoc),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3sharedemocratize" = "Share democratizing members",
                          "lag3sharestabledemoc" = "Share stable democratic members",
                          "lag3shareautocratize" = "Share autocratizing members",
                          "lag3sharestableautoc" = "Share stable autocratic members",
                          "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Democratizers" = 2:4, "Stable democracies" = 5:7, "Autocratizers" = 8:10, "Stable autocracies" = 11:13))


### Table A7
modelsummary(list("Delegation" = delmodelintdemocratiz, "Pooling" = poolmodelintdemocratiz, "Access" = accmodelintdemocratiz,
                  "Delegation" = delmodelintstabledemoc, "Pooling" = poolmodelintstabledemoc,  "Access" = accmodelintstabledemoc, 
                  "Delegation" = delmodelintautocratiz, "Pooling" = poolmodelintautocratiz,  "Access" = accmodelintautocratiz, 
                  "Delegation" = delmodelintstableautoc, "Pooling" = poolmodelintstableautoc,  "Access" = accmodelintstableautoc),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3sharedemocratize" = "Share democratizing members", "lag3sharedemocratize:lag3contract::general purpose" = "Share democratizing members*Governance purpose (ref: Task-specific)",
                          "lag3sharestabledemoc" = "Share stable democratic members", "lag3sharestabledemoc:lag3contract::general purpose" = "Share stable democratic members*Governance purpose (ref: Task-specific)",
                          "lag3shareautocratize" = "Share autocratizing members", "lag3shareautocratize:lag3contract::general purpose" = "Share autocratizing members*Governance purpose (ref: Task-specific)",
                          "lag3sharestableautoc" = "Share stable autocratic members", "lag3sharestableautoc:lag3contract::general purpose" = "Share stable autocratic members*Governance purpose (ref: Task-specific)",
                          "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Democratizers" = 2:4, "Stable democracies" = 5:7, "Autocratizers" = 8:10, "Stable autocracies" = 11:13))


### Figure A4; A5

# Create mfx for interaction

poolmodelintdemocratizplot <- avg_comparisons(poolmodelintdemocratiz, type = "response", variables = list("lag3sharedemocratize" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintdemocratiz, type = "wald"))
poolmodelintstabledemocplot <- avg_comparisons(poolmodelintstabledemoc, type = "response", variables = list("lag3sharestabledemoc" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintstabledemoc, type = "wald"))

poolmodelintautocratizplot <- avg_comparisons(poolmodelintautocratiz, type = "response", variables = list("lag3shareautocratize" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintautocratiz, type = "wald"))
poolmodelintstableautocplot <- avg_comparisons(poolmodelintstableautoc, type = "response", variables = list("lag3sharestableautoc" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintstableautoc, type = "wald"))

delmodelintdemocratizplot <- avg_comparisons(delmodelintdemocratiz, type = "response", variables = list("lag3sharedemocratize" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintdemocratiz, type = "wald"))
delmodelintstabledemocplot <- avg_comparisons(delmodelintstabledemoc, type = "response", variables = list("lag3sharestabledemoc" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintstabledemoc, type = "wald"))

delmodelintautocratizplot <- avg_comparisons(delmodelintautocratiz, type = "response", variables = list("lag3shareautocratize" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintautocratiz, type = "wald"))
delmodelintstableautocplot <- avg_comparisons(delmodelintstableautoc, type = "response", variables = list("lag3sharestableautoc" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintstableautoc, type = "wald"))

accmodelintdemocratizplot <- avg_comparisons(accmodelintdemocratiz, type = "response", variables = list("lag3sharedemocratize" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintdemocratiz, type = "wald"))
accmodelintstabledemocplot <- avg_comparisons(accmodelintstabledemoc, type = "response", variables = list("lag3sharestabledemoc" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintstabledemoc, type = "wald"))

accmodelintautocratizplot <- avg_comparisons(accmodelintautocratiz, type = "response", variables = list("lag3shareautocratize" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintautocratiz, type = "wald"))
accmodelintstableautocplot <- avg_comparisons(accmodelintstableautoc, type = "response", variables = list("lag3sharestableautoc" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintstableautoc, type = "wald"))


# Create legend data

legend_data_democstabch <- data.frame(
  term = c("lag3sharedemocratize", "lag3sharestabledemoc"), 
  label = c("Share democratizing members", "Share stable democratic members"),
  shape = c(16, 15))


# Create legend data

legend_data_autocstabch <- data.frame(
  term = c("lag3shareautocratize", "lag3sharestableautoc"), 
  label = c("Share autocratizing members", "Share stable autocratic members"),
  shape = c(16, 15))


# Create interaction plots

pdemstabch <- ggplot(poolmodelintdemocratizplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = poolmodelintstabledemocplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.025)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_democstabch$term, values = legend_data_democstabch$shape, labels = legend_data_democstabch$label) +
  ggtitle("Pooling")

pautstabch <- ggplot(poolmodelintautocratizplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = poolmodelintstableautocplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.025)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_autocstabch$term, values = legend_data_autocstabch$shape, labels = legend_data_autocstabch$label) +
  ggtitle("Pooling")

ddemstabch <- ggplot(delmodelintdemocratizplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = delmodelintstabledemocplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.025)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_democstabch$term, values = legend_data_democstabch$shape, labels = legend_data_democstabch$label) +
  ggtitle("Delegation")

dautstabch <- ggplot(delmodelintautocratizplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = delmodelintstableautocplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.025)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_autocstabch$term, values = legend_data_autocstabch$shape, labels = legend_data_autocstabch$label) +
  ggtitle("Delegation")

ademstabch <- ggplot(accmodelintdemocratizplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.3, 0.4), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = accmodelintstabledemocplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.025)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_democstabch$term, values = legend_data_democstabch$shape, labels = legend_data_democstabch$label) +
  ggtitle("Access")

aautstabch <- ggplot(accmodelintautocratizplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.3, 0.4), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = accmodelintstableautocplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.025)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_autocstabch$term, values = legend_data_autocstabch$shape, labels = legend_data_autocstabch$label) +
  ggtitle("Access")


### Figure A4

ddemstabch + pdemstabch + ademstabch + 
  plot_layout(ncol = 3, axis_titles = "collect", guides = "collect") & 
  theme(legend.position = "bottom", axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
        axis.title = element_text(size = 15), legend.text = element_text(size = 12))

ggsave("FigureA4.tiff", width = 14, height = 5.5, dpi = 600, compression = "lzw")


### Figure A5

dautstabch + pautstabch + aautstabch + 
  plot_layout(ncol = 3, axis_titles = "collect", guides = "collect") & 
  theme(legend.position = "bottom", axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
        axis.title = element_text(size = 15), legend.text = element_text(size = 12))

ggsave("FigureA5.tiff", width = 14, height = 5.5, dpi = 600, compression = "lzw")



##### Robustness checks with alternative lag structures

### Table A8; A9

## Alternative lag 1

# Direct relationship

poolmodelf <- feols(pooling ~ lag1elecdem + i(lag1contract, ref = "task specific") + lag1sdelecdem + log(lag1nmember) + lag1avggdppc + lag1sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelf <- feols(delegation ~ lag1elecdem + i(lag1contract, ref = "task specific") + lag1sdelecdem + log(lag1nmember) + lag1avggdppc + lag1sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelf <- feols(accessio ~ lag1elecdem + i(lag1contract, ref = "task specific") + lag1sdelecdem + log(lag1nmember) + lag1avggdppc + lag1sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintf <- feols(pooling ~ lag1elecdem*i(lag1contract, ref = "task specific") + lag1sdelecdem + log(lag1nmember) + lag1avggdppc + lag1sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintf <- feols(delegation ~ lag1elecdem*i(lag1contract, ref = "task specific") + lag1sdelecdem + log(lag1nmember) + lag1avggdppc + lag1sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintf <- feols(accessio ~ lag1elecdem*i(lag1contract, ref = "task specific") + lag1sdelecdem + log(lag1nmember) + lag1avggdppc + lag1sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Alternative lag 5

# Direct relationship

poolmodelg <- feols(pooling ~ lag5elecdem + i(lag5contract, ref = "task specific") + lag5sdelecdem + log(lag5nmember) + lag5avggdppc + lag5sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodelg <- feols(delegation ~ lag5elecdem + i(lag5contract, ref = "task specific") + lag5sdelecdem + log(lag5nmember) + lag5avggdppc + lag5sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelg <- feols(accessio ~ lag5elecdem + i(lag5contract, ref = "task specific") + lag5sdelecdem + log(lag5nmember) + lag5avggdppc + lag5sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintg <- feols(pooling ~ lag5elecdem*i(lag5contract, ref = "task specific") + lag5sdelecdem + log(lag5nmember) + lag5avggdppc + lag5sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelintg <- feols(delegation ~ lag5elecdem*i(lag5contract, ref = "task specific") + lag5sdelecdem + log(lag5nmember) + lag5avggdppc + lag5sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelintg <- feols(accessio ~ lag5elecdem*i(lag5contract, ref = "task specific") + lag5sdelecdem + log(lag5nmember) + lag5avggdppc + lag5sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


## Alternative lag 7

# Direct relationship

poolmodel7lag <- feols(pooling ~ lag7elecdem + i(lag7contract, ref = "task specific") + lag7sdelecdem + log(lag7nmember) + lag7avggdppc + lag7sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
delmodel7lag <- feols(delegation ~ lag7elecdem + i(lag7contract, ref = "task specific") + lag7sdelecdem + log(lag7nmember) + lag7avggdppc + lag7sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodel7lag <- feols(accessio ~ lag7elecdem + i(lag7contract, ref = "task specific") + lag7sdelecdem + log(lag7nmember) + lag7avggdppc + lag7sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelint7lag <- feols(pooling ~ lag7elecdem*i(lag7contract, ref = "task specific") + lag7sdelecdem + log(lag7nmember) + lag7avggdppc + lag7sdgdppc + year| ionumber, combandmia2, cluster = "ionumber")
delmodelint7lag <- feols(delegation ~ lag7elecdem*i(lag7contract, ref = "task specific") + lag7sdelecdem + log(lag7nmember) + lag7avggdppc + lag7sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")
accmodelint7lag <- feols(accessio ~ lag7elecdem*i(lag7contract, ref = "task specific") + lag7sdelecdem + log(lag7nmember) + lag7avggdppc + lag7sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


### Table A8

modelsummary(list("Delegation" = delmodelf, "Pooling" = poolmodelf,  "Access" = accmodelf, 
                  "Delegation" = delmodelg, "Pooling" = poolmodelg,  "Access" = accmodelg, 
                  "Delegation" = delmodel7lag, "Pooling" = poolmodel7lag,  "Access" = accmodel7lag),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag1elecdem" = "Democratic density", "lag1sdelecdem" = "Regime heterogeneity", "log(lag1nmember)" = "Membership size (ln)", "lag1avggdppc" = "Affluence",
                          "lag1sdgdppc" = "GDP heterogeneity", "lag1contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year",
                          "lag5elecdem" = "Democratic density", "lag5sdelecdem" = "Regime heterogeneity", "log(lag5nmember)" = "Membership size (ln)", "lag5avggdppc" = "Affluence",
                          "lag5sdgdppc" = "GDP heterogeneity", "lag5contract::general purpose" = "Governance purpose (ref: Task-specific)",
                          "lag7elecdem" = "Democratic density", "lag7sdelecdem" = "Regime heterogeneity", "log(lag7nmember)" = "Membership size (ln)", "lag7avggdppc" = "Affluence",
                          "lag7sdgdppc" = "GDP heterogeneity", "lag7contract::general purpose" = "Governance purpose (ref: Task-specific)"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("1-year lag" = 2:4, "5-year lag" = 5:7, "7-year lag" = 8:10))


### Table A9

modelsummary(list("Delegation" = delmodelintf, "Pooling" = poolmodelintf,  "Access" = accmodelintf, 
                  "Delegation" = delmodelintg, "Pooling" = poolmodelintg,  "Access" = accmodelintg, 
                  "Delegation" = delmodelint7lag, "Pooling" = poolmodelint7lag,  "Access" = accmodelint7lag),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag1elecdem" = "Democratic density", "lag1elecdem:lag1contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag1sdelecdem" = "Regime heterogeneity", "log(lag1nmember)" = "Membership size (ln)", "lag1avggdppc" = "Affluence",
                          "lag1sdgdppc" = "GDP heterogeneity", "lag1contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year",
                          "lag5elecdem" = "Democratic density", "lag5elecdem:lag5contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag5sdelecdem" = "Regime heterogeneity", "log(lag5nmember)" = "Membership size (ln)", "lag5avggdppc" = "Affluence",
                          "lag5sdgdppc" = "GDP heterogeneity", "lag5contract::general purpose" = "Governance purpose (ref: Task-specific)",
                          "lag7elecdem" = "Democratic density", "lag7elecdem:lag7contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag7sdelecdem" = "Regime heterogeneity", "log(lag7nmember)" = "Membership size (ln)", "lag7avggdppc" = "Affluence",
                          "lag7sdgdppc" = "GDP heterogeneity", "lag7contract::general purpose" = "Governance purpose (ref: Task-specific)"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("1-year lag" = 2:4, "5-year lag" = 5:7, "7-year lag" = 8:10))



### Figure A6

# Create mfx for interaction

delmodelintfplot <- avg_comparisons(delmodelintf, type = "response", variables = list("lag1elecdem" = 0.1), by = "lag1contract", df = insight::get_df(delmodelintf, type = "wald"))
delmodelintgplot <- avg_comparisons(delmodelintg, type = "response", variables = list("lag5elecdem" = 0.1), by = "lag5contract", df = insight::get_df(delmodelintg, type = "wald"))
delmodelint7lagplot <- avg_comparisons(delmodelint7lag, type = "response", variables = list("lag7elecdem" = 0.1), by = "lag7contract", df = insight::get_df(delmodelint7lag, type = "wald"))

poolmodelintfplot <- avg_comparisons(poolmodelintf, type = "response", variables = list("lag1elecdem" = 0.1), by = "lag1contract", df = insight::get_df(poolmodelintf, type = "wald"))
poolmodelintgplot <- avg_comparisons(poolmodelintg, type = "response", variables = list("lag5elecdem" = 0.1), by = "lag5contract", df = insight::get_df(poolmodelintg, type = "wald"))
poolmodelint7lagplot <- avg_comparisons(poolmodelint7lag, type = "response", variables = list("lag7elecdem" = 0.1), by = "lag7contract", df = insight::get_df(poolmodelint7lag, type = "wald"))

accmodelintfplot <- avg_comparisons(accmodelintf, type = "response", variables = list("lag1elecdem" = 0.1), by = "lag1contract", df = insight::get_df(accmodelintf, type = "wald"))
accmodelintgplot <- avg_comparisons(accmodelintg, type = "response", variables = list("lag5elecdem" = 0.1), by = "lag5contract", df = insight::get_df(accmodelintg, type = "wald"))
accmodelint7lagplot <- avg_comparisons(accmodelint7lag, type = "response", variables = list("lag7elecdem" = 0.1), by = "lag7contract", df = insight::get_df(accmodelint7lag, type = "wald"))


# Create legend data

legend_data_altlag <- data.frame(
  term = c("lag1elecdem", "lag5elecdem", "lag7elecdem"), 
  label = c("1-year lag", "5-year lag", "7-year lag"),
  shape = c(16, 15, 17))


# Create interaction plots

paltlagpool <- ggplot(poolmodelintfplot) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(x = lag1contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = poolmodelintgplot, aes(x = lag5contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0)) +
  geom_pointrange(data = poolmodelint7lagplot, aes(x = lag7contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.05)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_altlag$term, values = legend_data_altlag$shape, labels = legend_data_altlag$label) +
  ggtitle("Pooling")

paltlagdel <- ggplot(delmodelintfplot) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(x = lag1contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = delmodelintgplot, aes(x = lag5contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0)) +
  geom_pointrange(data = delmodelint7lagplot, aes(x = lag7contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0.05)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_altlag$term, values = legend_data_altlag$shape, labels = legend_data_altlag$label) +
  ggtitle("Delegation")

paltlagacc <- ggplot(accmodelintfplot) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.3, 0.4), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(x = lag1contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.1)) +
  geom_pointrange(data = accmodelintgplot, aes(x = lag5contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = accmodelint7lagplot, aes(x = lag7contract, y = estimate, ymin = conf.low, ymax = conf.high, shape = term), position = position_nudge(x = 0)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_altlag$term, values = legend_data_altlag$shape, labels = legend_data_altlag$label) +
  ggtitle("Access")

paltlagdel + paltlagpool + paltlagacc + 
  plot_layout(ncol = 3, axis_titles = "collect", guides = "collect") & 
  theme(legend.position = "bottom", axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
        axis.title = element_text(size = 15), legend.text = element_text(size = 12))

ggsave("figureA6.tiff", width = 14, height = 5.5, dpi = 600, compression = "lzw")


#### Alternative controls for time

### Table A10; A11

## Polynomial

# Direct relationship

poolmodelh <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + year^2 + year^3 | ionumber, combandmia2, cluster = "ionumber")
delmodelh <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + year^2 + year^3 | ionumber, combandmia2, cluster = "ionumber")
accmodelh <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + year^2 + year^3 | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelinth <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + year^2 + year^3 | ionumber, combandmia2, cluster = "ionumber")
delmodelinth <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + year^2 + year^3 | ionumber, combandmia2, cluster = "ionumber")
accmodelinth <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + year^2 + year^3 | ionumber, combandmia2, cluster = "ionumber")


## Post Cold War dummy

# Direct relationship

poolmodeli <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + i(postcw) | ionumber, combandmia2, cluster = "ionumber")
delmodeli <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + i(postcw) | ionumber, combandmia2, cluster = "ionumber")
accmodeli <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + i(postcw) | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelinti <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + i(postcw) | ionumber, combandmia2, cluster = "ionumber")
delmodelinti <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + i(postcw)| ionumber, combandmia2, cluster = "ionumber")
accmodelinti <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + i(postcw) | ionumber, combandmia2, cluster = "ionumber")


## Year fixed effects

# Direct relationship

poolmodelj <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc | ionumber + year, combandmia2, cluster = "ionumber")
delmodelj <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc | ionumber + year, combandmia2, cluster = "ionumber")
accmodelj <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc | ionumber + year, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintj <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc | ionumber + year, combandmia2, cluster = "ionumber")
delmodelintj <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc | ionumber + year, combandmia2, cluster = "ionumber")
accmodelintj <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc| ionumber + year, combandmia2, cluster = "ionumber")



## Individual time trends for different IO types

# Direct relationship

poolmodelTT <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year*i(lag3contract, ref = "task specific") | ionumber, combandmia2, cluster = "ionumber")
delmodelTT <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year*i(lag3contract, ref = "task specific") | ionumber, combandmia2, cluster = "ionumber")
accmodelTT <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year*i(lag3contract, ref = "task specific") | ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintTT <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year*i(lag3contract, ref = "task specific")| ionumber, combandmia2, cluster = "ionumber")
delmodelintTT <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year*i(lag3contract, ref = "task specific") | ionumber, combandmia2, cluster = "ionumber")
accmodelintTT <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year*i(lag3contract, ref = "task specific") | ionumber, combandmia2, cluster = "ionumber")


### Table A10

modelsummary(list("Delegation" = delmodelh, "Pooling" = poolmodelh,  "Access" = accmodelh, 
                  "Delegation" = delmodeli, "Pooling" = poolmodeli,  "Access" = accmodeli, 
                  "Delegation" = delmodelj, "Pooling" = poolmodelj,  "Access" = accmodelj, 
                  "Delegation" = delmodelTT, "Pooling" = poolmodelTT,  "Access" = accmodelTT),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "FE: year", clean = "Year fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year",
                          "I(year^2)" = "Year^2", "I(year^3)" = "Year^3", "postcw::1" = "Post-Cold War", "year:lag3contract::general purpose" = "Year*Governance purpose (ref: Task-specific)"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Cubic polynomial" = 2:4, "Post-Cold War dummy" = 5:7, "Year fixed effects" = 8:10, "Separate time trends" = 11:13))


### Table A11

modelsummary(list("Delegation" = delmodelinth, "Pooling" = poolmodelinth,  "Access" = accmodelinth, 
                  "Delegation" = delmodelinti, "Pooling" = poolmodelinti,  "Access" = accmodelinti, 
                  "Delegation" = delmodelintj, "Pooling" = poolmodelintj,  "Access" = accmodelintj, 
                  "Delegation" = delmodelintTT, "Pooling" = poolmodelintTT,  "Access" = accmodelintTT),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "FE: year", clean = "Year fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3elecdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year",
                          "I(year^2)" = "Year^2", "I(year^3)" = "Year^3", "postcw::1" = "Post-Cold War", "year:lag3contract::general purpose" = "Year*Governance purpose (ref: Task-specific)"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Cubic polynomial" = 2:4, "Post-Cold War dummy" = 5:7, "Year fixed effects" = 8:10, "Separate time trends" = 11:13))


### Figure A7

# Create mfx for interaction

delmodelinthplot <- avg_comparisons(delmodelinth, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelinth, type = "wald"))
delmodelinthplot$time <- "qp"
delmodelintiplot <- avg_comparisons(delmodelinti, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelinti, type = "wald"))
delmodelintiplot$time <- "cw"
delmodelintjplot <- avg_comparisons(delmodelintj, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintj, type = "wald"))
delmodelintjplot$time <- "fe"
delmodelintTTplot <- avg_comparisons(delmodelintTT, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelintTT, type = "wald"))
delmodelintTTplot$time <- "tt"

poolmodelinthplot <- avg_comparisons(poolmodelinth, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelinth, type = "wald"))
poolmodelinthplot$time <- "qp"
poolmodelintiplot <- avg_comparisons(poolmodelinti, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelinti, type = "wald"))
poolmodelintiplot$time <- "cw"
poolmodelintjplot <- avg_comparisons(poolmodelintj, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintj, type = "wald"))
poolmodelintjplot$time <- "fe"
poolmodelintTTplot <- avg_comparisons(poolmodelintTT, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelintTT, type = "wald"))
poolmodelintTTplot$time <- "tt"

accmodelinthplot <- avg_comparisons(accmodelinth, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelinth, type = "wald"))
accmodelinthplot$time <- "qp"
accmodelintiplot <- avg_comparisons(accmodelinti, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelinti, type = "wald"))
accmodelintiplot$time <- "cw"
accmodelintjplot <- avg_comparisons(accmodelintj, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintj, type = "wald"))
accmodelintjplot$time <- "fe"
accmodelintTTplot <- avg_comparisons(accmodelintTT, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelintTT, type = "wald"))
accmodelintTTplot$time <- "tt"


# Create legend data

legend_data_alttime <- data.frame(
  time = c("qp", "cw", "fe", "tt"), 
  label = c("Cubic polynomial", "Post-Cold War dummy", "Year fixed effects", "Separate trends"),
  shape = c(16, 15, 17, 18))


# Create interaction plots

palttimepool <- ggplot(poolmodelinthplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = -0.075)) +
  geom_pointrange(data = poolmodelintiplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = poolmodelintjplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = 0.025)) +
  geom_pointrange(data = poolmodelintTTplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = 0.075)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_alttime$time, values = legend_data_alttime$shape, labels = legend_data_alttime$label) +
  ggtitle("Pooling")

palttimedel <- ggplot(delmodelinthplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = -0.075)) +
  geom_pointrange(data = delmodelintiplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = delmodelintjplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = 0.025)) +
  geom_pointrange(data = delmodelintTTplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = 0.075)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_alttime$time, values = legend_data_alttime$shape, labels = legend_data_alttime$label) +
  ggtitle("Delegation")

palttimeacc <- ggplot(accmodelinthplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.3, 0.4), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = -0.075)) +
  geom_pointrange(data = accmodelintiplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = -0.025)) +
  geom_pointrange(data = accmodelintjplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = 0.025)) +
  geom_pointrange(data = accmodelintTTplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = time), position = position_nudge(x = 0.075)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_alttime$time, values = legend_data_alttime$shape, labels = legend_data_alttime$label) +
  ggtitle("Access")

palttimedel + palttimepool + palttimeacc + 
  plot_layout(ncol = 3, axis_titles = "collect", guides = "collect") & 
  theme(legend.position = "bottom", axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
        axis.title = element_text(size = 15), legend.text = element_text(size = 12))

ggsave("FigureA7.tiff", width = 14, height = 5.5, dpi = 600, compression = "lzw")


#### Pooled OLS

### Table A12

# Direct effect

poolmodelOLS <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year, combandmia2, cluster = "ionumber")
delmodelOLS <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year, combandmia2, cluster = "ionumber")
accmodelOLS <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintOLS <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year, combandmia2, cluster = "ionumber")
delmodelintOLS <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year, combandmia2, cluster = "ionumber")
accmodelintOLS <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year, combandmia2, cluster = "ionumber")

modelsummary(list(delmodelOLS, poolmodelOLS, accmodelOLS, delmodelintOLS, poolmodelintOLS,  accmodelintOLS),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("(Intercept)" = "(Intercept)", "lag3elecdem" = "Democratic density", "lag3elecdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.")


### Figure A8


poolplotOLS <- plot_comparisons(poolmodelintOLS, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(poolmodelintOLS, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.05,0.1)) + ggtitle("Pooling") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotOLS <- plot_comparisons(delmodelintOLS, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(delmodelintOLS, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.05,0.1)) + ggtitle("Delegation") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

accplotOLS <- plot_comparisons(accmodelintOLS, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(accmodelintOLS, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.15,0.3)) + ggtitle("Access") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotOLS + poolplotOLS + accplotOLS + plot_layout(axis_titles = "collect") & theme(axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
                                                                                     axis.title = element_text(size = 15))

ggsave("FigureA8.tiff", width = 11, height = 4, dpi = 600, compression = "lzw")



#### Split sample

### Table A13

poolmodelkgp <- feols(pooling ~ lag3elecdem + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "general purpose"), cluster = "ionumber")
poolmodelkts <- feols(pooling ~ lag3elecdem + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")

delmodelkgp <- feols(delegation ~ lag3elecdem + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "general purpose"), cluster = "ionumber")
delmodelkts <- feols(delegation ~ lag3elecdem + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")

accmodelkgp <- feols(accessio ~ lag3elecdem + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "general purpose"), cluster = "ionumber")
accmodelkts <- feols(accessio ~ lag3elecdem + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")

modelsummary(list("Delegation (task-specific)" = delmodelkts, "Delegation (general-purpose)" = delmodelkgp,  
                  "Pooling (task-specific)" = poolmodelkts, "Pooling (general-purpose)" = poolmodelkgp,
                  "Access (task-specific)" = accmodelkts, "Access (general-purpose)" = accmodelkgp),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO Fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density","lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.")


#### Alternative samples

### Table A14; A15

## Remove uncertain coding

# Direct relationship

accmodelnouncertaindir <- feols(accessnouncertain ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")


# Pooling and delegation

poolmodelnouncertaindir <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 1563 & ionumber != 2530), cluster = "ionumber")
delmodelnouncertaindir <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 1563 & ionumber != 2530), cluster = "ionumber")


## Remove IOs with many missing small countries on V-dem and GDP per cap (CARICOM (V-dem), OAPEC (GDP), SPC (GDP), PIF (GDP and some V-dem)).

# Direct relationship

accmodelremovecaricomdir <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 880), cluster = "ionumber")
poolmodelremovecaricomdir <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 880), cluster = "ionumber")
delmodelremovecaricomdir <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 880), cluster = "ionumber")

accmodelremoveoapecdir <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 3800), cluster = "ionumber")
poolmodelremoveoapecdir <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 3800), cluster = "ionumber")
delmodelremoveoapecdir <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 3800), cluster = "ionumber")

accmodelremovespcdir <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4190), cluster = "ionumber")
poolmodelremovespcdir <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 4190), cluster = "ionumber")
delmodelremovespcdir <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4190), cluster = "ionumber")

accmodelremovepifdir <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4200), cluster = "ionumber")
poolmodelremovepifdir <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 4200), cluster = "ionumber")
delmodelremovepifdir <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4200), cluster = "ionumber")


## Remove uncertain coding

# Interaction

# Access

accmodelnouncertain <- feols(accessnouncertain ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2, cluster = "ionumber")

# Pooling and delegation

poolmodelnouncertain <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 1563 & ionumber != 2530), cluster = "ionumber")
delmodelnouncertain <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 1563 & ionumber != 2530), cluster = "ionumber")


## Remove IOs with many missing small countries on V-dem and GDP per cap (CARICOM (V-dem), OAPEC (GDP), SPC (GDP), PIF (GDP and some V-dem)).

# Interaction

accmodelremovecaricom <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 880), cluster = "ionumber")
poolmodelremovecaricom <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 880), cluster = "ionumber")
delmodelremovecaricom <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 880), cluster = "ionumber")

accmodelremoveoapec <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 3800), cluster = "ionumber")
poolmodelremoveoapec <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 3800), cluster = "ionumber")
delmodelremoveoapec <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 3800), cluster = "ionumber")

accmodelremovespc <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4190), cluster = "ionumber")
poolmodelremovespc <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 4190), cluster = "ionumber")
delmodelremovespc <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4190), cluster = "ionumber")

accmodelremovepif <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4200), cluster = "ionumber")
poolmodelremovepif <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, subset(combandmia2, ionumber != 4200), cluster = "ionumber")
delmodelremovepif <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, ionumber != 4200), cluster = "ionumber")



### Table A14

modelsummary(list("Delegation" = delmodelnouncertaindir, "Pooling" = poolmodelnouncertaindir, "Access" = accmodelnouncertaindir, 
                  "Delegation" = delmodelremovecaricomdir, "Pooling" = poolmodelremovecaricomdir, "Access" = accmodelremovecaricomdir,
                  "Delegation" = delmodelremoveoapecdir, "Pooling" = poolmodelremoveoapecdir, "Access" = accmodelremoveoapecdir,
                  "Delegation" = delmodelremovespcdir, "Pooling" = poolmodelremovespcdir, "Access" = accmodelremovespcdir,
                  "Delegation" = delmodelremovepifdir, "Pooling" = poolmodelremovepifdir, "Access" = accmodelremovepifdir),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Dropping parts of new coding" = 2:4, "Dropping CARICOM" = 5:7, "Dropping OAPEC" = 8:10,
                    "Dropping SPC" = 11:13, "Dropping PIF" = 14:16))


### Table A15

modelsummary(list("Delegation" = delmodelnouncertain, "Pooling" = poolmodelnouncertain, "Access" = accmodelnouncertain, 
                  "Delegation" = delmodelremovecaricom, "Pooling" = poolmodelremovecaricom, "Access" = accmodelremovecaricom,
                  "Delegation" = delmodelremoveoapec, "Pooling" = poolmodelremoveoapec, "Access" = accmodelremoveoapec,
                  "Delegation" = delmodelremovespc, "Pooling" = poolmodelremovespc, "Access" = accmodelremovespc,
                  "Delegation" = delmodelremovepif, "Pooling" = poolmodelremovepif, "Access" = accmodelremovepif),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3elecdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Dropping parts of new coding" = 2:4, "Dropping CARICOM" = 5:7, "Dropping OAPEC" = 8:10,
                    "Dropping SPC" = 11:13, "Dropping PIF" = 14:16))


### Figure A9

# Create mfx for interaction

poolmodelnouncertainplot <- avg_comparisons(poolmodelnouncertain, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelnouncertain, type = "wald"))
poolmodelnouncertainplot$sample <- "noun"
poolmodelremovecaricomplot  <- avg_comparisons(poolmodelremovecaricom, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelremovecaricom, type = "wald"))
poolmodelremovecaricomplot$sample <- "nocar"
poolmodelremoveoapecplot  <- avg_comparisons(poolmodelremoveoapec, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelremoveoapec, type = "wald"))
poolmodelremoveoapecplot$sample <- "nooap"
poolmodelremovespcplot  <- avg_comparisons(poolmodelremovespc, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelremovespc, type = "wald"))
poolmodelremovespcplot$sample <- "nospc"
poolmodelremovepifplot  <- avg_comparisons(poolmodelremovepif, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(poolmodelremovepif, type = "wald"))
poolmodelremovepifplot$sample <- "nopif"

delmodelnouncertainplot <- avg_comparisons(delmodelnouncertain, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelnouncertain, type = "wald"))
delmodelnouncertainplot$sample <- "noun"
delmodelremovecaricomplot <- avg_comparisons(delmodelremovecaricom, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelremovecaricom, type = "wald"))
delmodelremovecaricomplot$sample <- "nocar"
delmodelremoveoapecplot <- avg_comparisons(delmodelremoveoapec, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelremoveoapec, type = "wald"))
delmodelremoveoapecplot$sample <- "nooap"
delmodelremovespcplot <- avg_comparisons(delmodelremovespc, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelremovespc, type = "wald"))
delmodelremovespcplot$sample <- "nospc"
delmodelremovepifplot <- avg_comparisons(delmodelremovepif, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(delmodelremovepif, type = "wald"))
delmodelremovepifplot$sample <- "nopif"

accmodelnouncertainplot <- avg_comparisons(accmodelnouncertain, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelnouncertain, type = "wald"))
accmodelnouncertainplot$sample <- "noun"
accmodelremovecaricomplot <- avg_comparisons(accmodelremovecaricom, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelremovecaricom, type = "wald"))
accmodelremovecaricomplot$sample <- "nocar"
accmodelremoveoapecplot <- avg_comparisons(accmodelremoveoapec, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelremoveoapec, type = "wald"))
accmodelremoveoapecplot$sample <- "nooap"
accmodelremovespcplot <- avg_comparisons(accmodelremovespc, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelremovespc, type = "wald"))
accmodelremovespcplot$sample <- "nospc"
accmodelremovepifplot <- avg_comparisons(accmodelremovepif, type = "response", variables = list("lag3elecdem" = 0.1), by = "lag3contract", df = insight::get_df(accmodelremovepif, type = "wald"))
accmodelremovepifplot$sample <- "nopif"


# Create legend data

legend_data_altsample <- data.frame(
  sample = c("noun", "nocar", "nooap", "nospc", "nopif"), 
  label = c("Dropping parts of new coding", "Dropping CARICOM", "Dropping OAPEC", "Dropping SPC", "Dropping PIF"),
  shape = c(16, 15, 17, 18, 0))


# Create interaction plots

paltsamplepool <- ggplot(poolmodelnouncertainplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = -0.1)) +
  geom_pointrange(data = poolmodelremovecaricomplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = poolmodelremoveoapecplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0)) +
  geom_pointrange(data = poolmodelremovespcplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0.05)) +
  geom_pointrange(data = poolmodelremovepifplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0.1)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_altsample$sample, values = legend_data_altsample$shape, labels = legend_data_altsample$label) +
  ggtitle("Pooling")

paltsampledel <- ggplot(delmodelnouncertainplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.075, 0.10), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = -0.1)) +
  geom_pointrange(data = delmodelremovecaricomplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = delmodelremoveoapecplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0)) +
  geom_pointrange(data = delmodelremovespcplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0.05)) +
  geom_pointrange(data = delmodelremovepifplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0.1)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_altsample$sample, values = legend_data_altsample$shape, labels = legend_data_altsample$label) +
  ggtitle("Delegation")

paltsampleacc <- ggplot(accmodelnouncertainplot, aes(x = lag3contract)) +
  scale_x_discrete(limits = c("task specific", "general purpose"), labels = c("Task-specific", "General-purpose"), name = "") +
  scale_y_continuous(limits = c(-0.3, 0.4), name = "Marginal effect") +
  geom_hline(yintercept = 0) +
  geom_pointrange(aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = -0.1)) +
  geom_pointrange(data = accmodelremovecaricomplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = -0.05)) +
  geom_pointrange(data = accmodelremoveoapecplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0)) +
  geom_pointrange(data = accmodelremovespcplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0.05)) +
  geom_pointrange(data = accmodelremovepifplot, aes(y = estimate, ymin = conf.low, ymax = conf.high, shape = sample), position = position_nudge(x = 0.1)) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_shape_manual(name = "", limits = legend_data_altsample$sample, values = legend_data_altsample$shape, labels = legend_data_altsample$label) +
  ggtitle("Access")

paltsampledel + paltsamplepool + paltsampleacc + 
  plot_layout(ncol = 3, axis_titles = "collect", guides = "collect") & 
  theme(legend.position = "bottom", axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
        axis.title = element_text(size = 15), legend.text = element_text(size = 12))


ggsave("FigureA9.tiff", width = 14, height = 5.5, dpi = 600, compression = "lzw")


#### Include pooling (constitutional change) in models to account for differences among IOs in terms of changes to treaties

### Table A16

# Direct relationship

poolmodelconst <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + lag3poolconstit| ionumber, combandmia2, cluster = "ionumber")
delmodelconst <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year  + lag3poolconstit| ionumber, combandmia2, cluster = "ionumber")
accmodelconst <- feols(accessio ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year  + lag3poolconstit| ionumber, combandmia2, cluster = "ionumber")

# Interaction

poolmodelintconst <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + lag3poolconstit| ionumber, combandmia2, cluster = "ionumber")
delmodelintconst <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year  + lag3poolconstit| ionumber, combandmia2, cluster = "ionumber")
accmodelintconst <- feols(accessio ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year + lag3poolconstit | ionumber, combandmia2, cluster = "ionumber")


# Create table

modelsummary(list("Delegation" = delmodelconst, "Pooling" = poolmodelconst, "Access" = accmodelconst, "Delegation" = delmodelintconst, "Pooling" = poolmodelintconst,  "Access" = accmodelintconst),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3elecdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year", "lag3poolconstit" = "Pooling (constitutional change)"),
             notes = "Standard errors clustered on IOs in parentheses.")


### Figure A10

poolplotintconst <- plot_comparisons(poolmodelintconst, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(poolmodelintconst, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Pooling") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotintconst <- plot_comparisons(delmodelintconst, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(delmodelintconst, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Delegation") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

accplotintconst <- plot_comparisons(accmodelintconst, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(accmodelintconst, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.15,0.3)) + ggtitle("Access") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotintconst + poolplotintconst + accplotintconst + plot_layout(axis_titles = "collect") & theme(axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
                                                                                                    axis.title = element_text(size = 15))

ggsave("FigureA10.tiff", width = 11, height = 4, dpi = 600, compression = "lzw")


#### Estimate models for task specific IOs with interaction for mandates authoritarian states may dislike

### Table A17

# Core state powers

poolmodelintTScorestate <- feols(pooling ~ lag3elecdem*i(lag3corestatecore, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")
delmodelintTSHRcorestate <- feols(delegation ~ lag3elecdem*i(lag3corestatecore, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")
accmodelintTSHRcorestate <- feols(accessio ~ lag3elecdem*i(lag3corestatecore, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")

# Human rights mandate

poolmodelintTSHR <- feols(pooling ~ lag3elecdem*i(lag3humrightcore, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")
delmodelintTSHR <- feols(delegation ~ lag3elecdem*i(lag3humrightany, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")
accmodelintTSHR <- feols(accessio ~ lag3elecdem*i(lag3humrightany, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")


# Human rights as core policy

poolmodelintTSHRcore <- feols(pooling ~ lag3elecdem*i(lag3humrightcore, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")
delmodelintTSHRcore <- feols(delegation ~ lag3elecdem*i(lag3humrightcore, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")
accmodelintTSHRcore <- feols(accessio ~ lag3elecdem*i(lag3humrightcore, ref = 0) + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, subset(combandmia2, lag3contract == "task specific"), cluster = "ionumber")

modelsummary(list("Delegation" = delmodelintTSHRcorestate, "Pooling" = poolmodelintTScorestate,  "Access" = accmodelintTSHRcorestate, 
                  "Delegation" = delmodelintTSHR, "Pooling" = poolmodelintTSHR,  "Access" = accmodelintTSHR, 
                  "Delegation" = delmodelintTSHRcore, "Pooling" = poolmodelintTSHRcore,  "Access" = accmodelintTSHRcore),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3elecdem:lag3corestatecore::1" = "Democratic density*Core state powers", 
                          "lag3elecdem:lag3humrightany::1" = "Democratic density*Human rights mandate",
                          "lag3elecdem:lag3humrightcore::1" = "Democratic density*Human rights core policy",
                          "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3corestatecore::1" = "Core state powers", 
                          "lag3humrightany::1" = "Human rights mandate",
                          "lag3humrightcore::1" = "Human rights core policy",
                          "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.") |>
  group_tt(j = list("Core state powers" = 2:4, "Human rights mandate" = 5:7, "Human rights core policy" = 8:10))


#### Models with full MIA sample

### Table A18

# Direct effect

poolmodelmia <- feols(pooling ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2b, cluster = "ionumber")
delmodelmia <- feols(delegation ~ lag3elecdem + i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2b, cluster = "ionumber")

# Interaction

poolmodelintmia <- feols(pooling ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year| ionumber, combandmia2b, cluster = "ionumber")
delmodelintmia <- feols(delegation ~ lag3elecdem*i(lag3contract, ref = "task specific") + lag3sdelecdem + log(lag3nmember) + lag3avggdppc + lag3sdgdppc + year | ionumber, combandmia2b, cluster = "ionumber")

# Construct table

modelsummary(list("Delegation" = delmodelmia, "Pooling" = poolmodelmia, "Delegation" = delmodelintmia, "Pooling" = poolmodelintmia),
             stars = c("**"=0.01, "*"=0.05),
             gof_map = list(list(raw = "FE: ionumber", clean = "IO fixed effects", fmt = 0),
                            list(raw = "nobs", clean = "Observations", fmt = 0)),
             coef_map = c("lag3elecdem" = "Democratic density", "lag3elecdem:lag3contract::general purpose" = "Democratic density*Governance purpose (ref: Task-specific)", "lag3sdelecdem" = "Regime heterogeneity", "log(lag3nmember)" = "Membership size (ln)", "lag3avggdppc" = "Affluence",
                          "lag3sdgdppc" = "GDP heterogeneity", "lag3contract::general purpose" = "Governance purpose (ref: Task-specific)", "year" = "Year"),
             notes = "Standard errors clustered on IOs in parentheses.")


### Figure A11

# Marginal effects

poolplotmia <- plot_comparisons(poolmodelintmia, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(poolmodelintmia, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Pooling") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotmia <- plot_comparisons(delmodelintmia, type = "response", variables = list("lag3elecdem" = 0.1), condition = "lag3contract", df = insight::get_df(delmodelintmia, type = "wald"))  + theme_bw() + theme(panel.grid = element_blank())+ 
  scale_y_continuous(name = "Marginal effect",limits = c(-0.04,0.08)) + ggtitle("Delegation") +
  scale_x_discrete(name = "", labels= c("Task-specific", "General-purpose"), limits = c("task specific", "general purpose")) + geom_hline(yintercept = 0, lty = "solid")

delplotmia + poolplotmia + plot_layout(axis_titles = "collect") & theme(axis.text = element_text(colour = "black", size = 13), plot.title = element_text(size = 16, hjust = 0.5, face = "bold"), 
                                                                        axis.title = element_text(size = 15))

ggsave("FigureA11.tiff", width = 8, height = 4, dpi = 600, compression = "lzw")




